package id.co.rho.scoring.cid.client.utils;

import com.google.gwt.user.client.rpc.AsyncCallback;

import id.co.rho.scoring.cid.client.command.CommandAction;
import id.co.rho.scoring.cid.client.command.CommandDispatcherAsync;
import id.co.rho.scoring.cid.client.command.CommandResult;

/**
 * Created by IntelliJ IDEA.
 * User: bungrudi
 * Date: 10/6/11
 * Time: 7:33 AM
 */
public class BlockingCall<T extends CommandResult> {
    private static final int DEFAULT_SLEEP_TIME = 100;

    private CommandDispatcherAsync commandDispatcher;
    private int sleepTime = DEFAULT_SLEEP_TIME;
    private CommandAction<T> command;

    Throwable caught;
    T result;
    volatile boolean returned = false;

    public BlockingCall(CommandDispatcherAsync commandDispatcher, CommandAction<T> command) {
        this.commandDispatcher = commandDispatcher;
        this.command = command;
    }

//    public T call() {
//        AsyncCallback callback = new AsyncCallback<T>() {
//            @Override
//            public void onFailure(Throwable caught) {
//                BlockingCall.this.caught = caught;
//                returned = true;
//            }
//
//            @Override
//            public void onSuccess(T result) {
//                BlockingCall.this.result = result;
//                returned = true;
//            }
//        };
//
//        commandDispatcher.execute(command, callback);
//        while(!returned) {
//            Thread.sleep(sleepTime);
//        }
//    }
}
